package com.toutou.base.bindhelper

import android.view.View
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.fragment.app.Fragment

class ViewGroupBDAdapter {
    companion object{
        @JvmStatic
        @BindingAdapter(value = ["childGroup"], requireAll = false)
        fun setChildGroup(viewGroup: ViewGroup, childGroup: List<View>){
            viewGroup.removeAllViews()
            for (view in childGroup) {
                viewGroup.addView(view)
            }
        }

        @JvmStatic
        @BindingAdapter(value = ["fragment", "fragmentManager"], requireAll = true)
        fun  setFragment(viewGroup: ViewGroup, fragment: Fragment?, fragmentManager: androidx.fragment.app.FragmentManager){
            val transaction = fragmentManager.beginTransaction()
            if(fragment == null){
                if(viewGroup.tag != null && viewGroup.tag is Fragment){
                    transaction.remove(viewGroup.tag as Fragment)
                    transaction.commit()
                }
            } else {
                viewGroup.tag = fragment
                transaction.replace(viewGroup.id, viewGroup.tag as Fragment)
                transaction.commit()
            }
        }
    }
}